package com.config;


import com.Service.LoginService;
import com.entity.Login;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class FileterController implements Filter {

    @Autowired
    private LoginService loginService;
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("Filter初始化中");
    }
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
                         FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        HttpSession session = request.getSession(true);
        String url=request.getRequestURI();
        System.err.println(url);
        if(url!=null && !url.equals("") && ( url.indexOf("Login")<0 && url.indexOf("login")<0 )) {
            Object name = session.getAttribute("name");
            System.err.println("name====="+name);
            if(null==name){
                System.err.println("bbbbbbbbbbbbbbbb");
                response.sendRedirect(request.getContextPath() + "/new/login.html");
                return ;
            }
            System.err.println("aaaaaaaaa");
            filterChain.doFilter(servletRequest, servletResponse);
        }else{
            System.err.println("dddddddddddddddddddd");
                /*String password = request.getHeader("password");
                //调用该方法后，表示过滤器经过原来的url请求处理方法
                Login login = new Login();
                login.setName(name);
                loginService.check(login);*/
            filterChain.doFilter(servletRequest, servletResponse);
        }
    }

    @Override
    public void destroy() {
        System.out.println("Filter销毁中");
    }
}